package com.muyu.danmo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoNews;
import com.muyu.danmo.service.IDanmoNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 时政要闻Controller
 *
 * @author yangle
 * @date 2024-09-19
 */
@RestController
@RequestMapping("/news")
public class DanmoNewsController extends BaseController
{
    @Autowired
    private IDanmoNewsService danmoNewsService;

    /**
     * 查询时政要闻列表
     */
//    @RequiresPermissions("system:news:list")
    @PostMapping("/list")
    public Result<PageInfo<DanmoNews>> list(@RequestBody DanmoNews danmoNews)
    {
        PageHelper.startPage(danmoNews.getPageNum(), danmoNews.getPageSize());
        List<DanmoNews> list = danmoNewsService.selectDanmoNewsList(danmoNews);
        PageInfo<DanmoNews> danmoNewsPageInfo = new PageInfo<>(list);
        return Result.success(danmoNewsPageInfo);
    }

    /**
     * 导出时政要闻列表
     */
//    @RequiresPermissions("system:news:export")
    @Log(title = "时政要闻", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DanmoNews danmoNews)
    {
        List<DanmoNews> list = danmoNewsService.selectDanmoNewsList(danmoNews);
        ExcelUtil<DanmoNews> util = new ExcelUtil<DanmoNews>(DanmoNews.class);
        util.exportExcel(response, list, "时政要闻数据");
    }

    /**
     * 获取时政要闻详细信息
     */
//    @RequiresPermissions("system:news:query")
    @GetMapping(value = "/{newsId}")
    public Result getInfo(@PathVariable("newsId") Long newsId)
    {

        return danmoNewsService.selectDanmoNewsByNewsId(newsId);
    }

    /**
     * 新增时政要闻
     */
//    @RequiresPermissions("system:news:add")
    @Log(title = "时政要闻", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody DanmoNews danmoNews)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser==null){
            return Result.notLoggedIn("请登录");
        }
        Long userid = loginUser.getUserid();
        danmoNews.setNewsId(userid);
        return danmoNewsService.insertDanmoNews(danmoNews);
    }

    /**
     * 修改时政要闻
     */
//    @RequiresPermissions("system:news:edit")
    @Log(title = "时政要闻", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody DanmoNews danmoNews)
    {
        return danmoNewsService.updateDanmoNews(danmoNews);
    }

    /**
     * 删除时政要闻
     */
//    @RequiresPermissions("system:news:remove")
    @Log(title = "时政要闻", businessType = BusinessType.DELETE)
    @DeleteMapping("/{newsIds}")
    public Result remove(@PathVariable Long[] newsIds)
    {
        return danmoNewsService.deleteDanmoNewsByNewsIds(newsIds);
    }
}
